package leetcode.offer;

import leetcode.base.TreeNode;

import java.util.LinkedList;

/**
 * ClassName: MirrorTree
 * Description:leetcode-剑指offer 27. 二叉树的镜像
 * date: 2021-11-02 21:44
 *
 * @author liyifan
 */
public class MirrorTree {
    public TreeNode mirrorTree(TreeNode root) {
        // todo 应该可以优化一下
        TreeNode res = new TreeNode(root.val);
        res.left = recur(res, root.right);
        res.right = recur(res, root.left);
        return res;
    }

    public TreeNode recur(TreeNode parent, TreeNode copy) {
        if (parent != null && copy != null) {
            TreeNode cur = new TreeNode(copy.val);
            if (copy == null) {
                return null;
            }
            else {
                if (copy.left != null)
                    cur.right = recur(cur, copy.left);
                if (copy.right != null)
                    cur.left = recur(cur, copy.right);
                return cur;
            }
        } else {
            return null;
        }
    }
}
